<?php
// 客户类
class CustomerController extends HomeController {
	public function _initialize(){	
		$this->_uid = session('uid');
		if(!$this->_uid) $this->display("Public:login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}
	public function index(){
	    $this->display();
	} 
	/*======================================================================*\
		Function:	customerList
		Purpose:	客户跟踪笔记列表
		Input:		无	            空
		Output:		display
	\*======================================================================*/
	public function customerList(){
		$GroupCustomer = M('GroupCustomer');
		$UserCustomer = M('UserCustomer');
		$uid = $this->_uid;
		$param = $this->_param();
		$target = $param['target'];
		$customerGroup = $GroupCustomer->select();
		if($target){
			$customerGroupId = $GroupCustomer->where("target = '$target'")->find();
			$id = $customerGroupId['id'];
			$customerList = $UserCustomer->where("group_id = $id AND owner_uid = $uid")->order('update_time desc')->select();
		}else{
			$customerList = $UserCustomer->alias('A')->join('oa_group_customer B ON A.group_id = B.id')->where("A.owner_uid = $uid")->order('A.update_time desc')->select();
		}
		//dump($customerList);
		$this->assign('group',$customerGroup);
		$this->assign('customerlist',$customerList);
		if($target){
			$this->display('customer');
			return;
		}
		$this->display();
	}
	/*public function customerListbak(){
		$GroupCustomer = M('GroupCustomer');
		$UserCustomer = M('UserCustomer');
		$uid = $this->_uid;
		$param = $this->_param();
		$customerGroup = $GroupCustomer->select();
		if($param['group_id'] && $param['group_id'] != "-2"){
			foreach($customerGroup as $key => $value){
				if($param['group_id'] == $value['id']){
					$customerList[0] = $value;
					break;
				}
			}
		}else{
			$customerList = $customerGroup;
		}
		//dump($customerList);
		foreach($customerList as $key => $value){
			$id = $value['id'];
			$customerList[$key]['customer'] = $UserCustomer->alias('A')
														   ->join('oa_customer B ON A.customer_id = B.customer_id')
														   ->field('A.*,B.remark')
														   ->where("A.group_id = $id AND A.owner_uid = $uid")->select();
		}
		//dump($customerList);
		$this->assign('group',$customerGroup);
		$this->assign('customerlist',$customerList);
		$this->display();
	}*/
	/******************************************
	*function:添加客户
	*参数:无
	*
	******************************************/
	public function addCustomer(){
		$GroupCustomer = M('GroupCustomer');
		$customerGroup = $GroupCustomer->order("id desc")->select();
		$this->assign('group',$customerGroup);
		$type = $_GET["type"];
		$uid = $this->_uid;
		$param = $this->_param();
		$typelist = array("addCustomer","individual");
		if($type && !in_array($type,$typelist)){
			$this->ajaxReturn(0,'系统出错，请联系管理员',0);
			return;
		}
		//echo $uid."123";
		if($type && $type == 'addCustomer'){
			$UserCustomer = M('UserCustomer');
			$data = array(
				'customer_name' => $param['customer_name'],
				'owner_uid' => $uid,
				'group_id' => $param['customer_groupid'],
				'create_time' => datetime(),
				'update_time' => datetime(),
				'status' => 1
			);
			$v = $UserCustomer->add($data);
			if($v){
				$this->ajaxReturn($v,'添加客户成功',1);
			}else{
				$this->ajaxReturn(0,'添加客户失败',0);
			}
		}
		if($type && $type == "individual"){
			$Customer = M('Customer');
			$param = $this->_param();
			if($_GET['control'] && $_GET['control'] == '1'){
				$v = $Customer->add($param);
				if($v){
					$this->ajaxReturn(1,'添加客户信息成功',1);
				}else{
					$this->ajaxReturn(0,'添加客户信息失败',0);
				}
			}
			$this->display('individual');
			return;
		}
		$this->display();
	}
	/******************************************
	*function:查看/添加跟踪笔记时，
	*		  用于用户查看该客户的基本资料
	*		  该客户的跟踪记录
	*参数:无
	*
	******************************************/
	public function customerInfo(){
		$Customer = M('Customer');
		$CustomerTracker = M('CustomerTracker');
		$param = $this->_param();
		$type = $param['type'];
		$customer_id = $param['customer_id'];
		$list = $Customer->where("customer_id = $customer_id")->find();
		if($type && $type == "tracklist"){
			$trackList = $CustomerTracker->alias('A')->join('oa_user_profile B ON A.track_uid = B.owner_uid')
										 ->field('A.*,B.name')->where("A.customer_id = $customer_id")->order("create_time asc")->select();
			$this->assign("tracklist",$trackList);
			//dump($trackList);
			$this->display('tracklist');
			return;
		}
		$this->assign("list",$list);
		$this->display();
	}
	/******************************************
	*function:添加跟踪记录
	*参数:无
	*
	******************************************/
	public function addTrack(){
		$param = $this->_param();
		$uid = $this->_uid;
		$CustomerTracker = M('CustomerTracker');
		$UserCustomer = M('UserCustomer');
		$customer_id = $param['customer_id'];
		$data = array(
			'track_uid' => $uid,
			'customer_id' => $customer_id,
			'track_content' => $param['content'],
			'create_time' => datetime()
		);
		$tdata['update_time'] = datetime();
		$track_num = $UserCustomer->where("customer_id = $customer_id")->setInc('track_num',1);
		if(!$track_num){
			$this->ajaxReturn(-1,'记录数增加出错',-1);
		}
		$t = $UserCustomer->where("customer_id = $customer_id")->save($tdata);
		if(!$t){
			$this->ajaxReturn(-1,'更新记录时间出错',-1);
		}
		$v = $CustomerTracker->add($data);
		if($v){
			$this->ajaxReturn(1,'添加记录成功',1);
		}else{
			$this->ajaxReturn(0,'添加记录失败',0);
		}
	}
}